home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / wtjmarch.zip / LIFE.ZIP / POLY12.COD < prev   
Text File  |  1991-12-09  |  6KB  |  175 lines

  1. ** Listing 1
  2. ** TLifeWindow object definition in Pascal
  3.  
  4. TLifeWindow = object(TWindow)
  5.   cells : TLifeCells;            { cells being mutated }
  6.   speed : Integer;               { timer speed         }
  7.   running : Boolean;             { is timer running?   }
  8.   rows : Integer;                { visible rows        }
  9.   cols : Integer;                { visible columns     }
  10.   gridSize : Integer;            { for drawing a cell  }
  11.   mouseDown : Boolean;           { is mouse down?      }
  12.   xDown : Integer;               { x location in grid  }
  13.   yDown : Integer;               { y location in grid  }
  14.   mutateDC : HDC;                { draw each mutation  }
  15.   mouseMoveDC : HDC;             { draw mouse moves    }
  16.   ...
  17.   { menu response methods }
  18.   procedure Clear(var Msg: TMessage); virtual cm_First + cm_Clear;
  19.   procedure Go(var Msg: TMessage); virtual cm_First + cm_Go;
  20.   procedure Stop(var Msg: TMessage); virtual cm_First + cm_Stop;
  21.   procedure About(var Msg: TMessage); virtual cm_First + cm_About;
  22.   ...
  23.   { windows message response methods }
  24.   procedure wmKeyDown(var Msg: TMessage); virtual wm_KeyDown;
  25.   procedure wmLButtonDown(var Msg: TMessage); virtual wm_LButtonDown;
  26.   procedure wmLButtonUp(var Msg: TMessage); virtual wm_LButtonUp;
  27.   procedure wmMouseMove(var Msg: TMessage); virtual wm_MouseMove;
  28.  ...
  29. end;
  30.  
  31. ** Listing 2
  32.  
  33. // C++ version of the TLifeWindow object
  34.  
  35. class TLifeWindow : public TWindow {
  36.   TLifeCells cells;            /* cells being mutated */
  37.   int speed;                   /* timer speed         */
  38.   Boolean running;             /* is timer running?   */
  39.   ...
  40. public:
  41.   /* menu response methods */
  42.   virtual void Clear(TMessage& Msg) = [ CM_FIRST + cm_Clear ];
  43.   virtual void Go(TMessage& Msg) = [ CM_FIRST + cm_Go ];
  44.   virtual void Stop(TMessage& Msg) = [ CM_FIRST + cm_Stop ];
  45.   ...
  46.   /* windows message response methods */
  47.   virtual void WMKeyDown(TMessage& Msg) = [ WM_FIRST + WM_KEYDOWN ];
  48.   virtual void WMLButtonDown(TMessage& Msg) = [WM_FIRST + WM_LBUTTONDOWN];
  49.   virtual void WMLButtonUp(TMessage& Msg) = [ WM_FIRST + WM_LBUTTONUP ];
  50.   virtual void WMMouseMove(TMessage& Msg) = [ WM_FIRST + WM_MOUSEMOVE ];
  51.   ...
  52. };
  53.  
  54. ** Listing 3
  55. ** Mouse response methods
  56.  
  57. { Begin capturing mouse movement when the left button is pressed.
  58.   A display context is taken; it is freed in the wmLButtonUp method.
  59. }
  60. procedure TLifeWindow.wmLButtonDown(var Msg: TMessage);
  61. begin
  62.   if not mouseDown then
  63.   begin
  64.     xDown := -1;     { sentinal values to track movement }
  65.     yDown := -1;
  66.     mouseDown := True;
  67.     mouseMoveDC := GetDC(HWindow);
  68.     selectObject(mouseMoveDC, GetStockObject(White_Pen));
  69.   end;
  70. end;
  71.  
  72. { Update the cells as the mouse is dragged }
  73. procedure TLifeWindow.WMMouseMove(var Msg: TMessage);
  74. var
  75.  xScreen, yScreen, x, y : Integer;
  76.  state : Boolean;
  77. begin
  78.   if mouseDown then
  79.   begin
  80.     { determine where clicked }
  81.     xScreen := Msg.LParamLo;
  82.     yScreen := Msg.LParamHi;
  83.     { translate into cell coordinates }
  84.     x := xScreen div gridSize;
  85.     y := yScreen div gridSize;
  86.     if (x <> xDown) or (y <> yDown) then      { a new position }
  87.     begin
  88.       { Invert the cell's state, then redraw }
  89.       xDown := x;                             { store position }
  90.       yDown := y;
  91.       state := not(cells.aliveCell(x, y));
  92.       cells.setCell(x, y, state);
  93.       cells.drawCell(mouseMoveDC, x, y, state)
  94.     end;
  95.   end;
  96. end;
  97.  
  98. { Stop capturing mouse movement when mouse is released }
  99. procedure TLifeWindow.wmLButtonUp(var Msg: TMessage);
  100. begin
  101.   wmMouseMove(Msg);  { force drawing in same spot }
  102.   if mouseDown then
  103.   begin
  104.     mouseDown := False;
  105.     selectObject(mouseMoveDC, GetStockObject(Black_Pen));
  106.     releaseDC(HWindow, mouseMoveDC);
  107.   end;
  108. end;
  109.  
  110. ** Listing 4
  111. ** Keyboard response method
  112.  
  113. { Use keyboard to simulate mouse events.  Accelerator keys
  114.   are handled as response methods. }
  115. procedure TLifeWindow.wmKeyDown(var Msg: TMessage);
  116. var x, y : Integer;
  117.     pos : TPoint;
  118.     key : word;
  119. begin
  120.   { Determine position of cursor in Window }
  121.   getCursorPos(pos);
  122.   screenToClient(HWindow, pos);
  123.   x:=pos.x;
  124.   y:=pos.y;
  125.   { move the cursor position }
  126.   key := Msg.WParam;
  127.   case key of
  128.     VK_UP    : y := y - gridSize;
  129.     VK_DOWN  : y := y + gridSize;
  130.     VK_RIGHT : x := x + gridSize;
  131.     VK_LEFT  : x := x - gridSize;
  132.     VK_HOME  :
  133.       begin
  134.        x := gridSize div 2;
  135.        y := gridSize div 2;
  136.       end;
  137.     VK_END :
  138.       begin
  139.        x := attr.w - gridSize div 2;
  140.        y := attr.h - gridSize div 2;
  141.       end;
  142.     VK_RETURN,
  143.     VK_SPACE :
  144.       begin
  145.         { Simulate mouse pressing at cursor position }
  146.         Msg.LParam := LongInt(pos);
  147.         wmLButtonDown(Msg);
  148.         wmLButtonUp(Msg);
  149.       end;
  150.     end;
  151.     { Update position of cursor in window with clipping }
  152.     if x < 0 then x := gridSize div 2;
  153.     if y < 0 then y := gridSize div 2;
  154.     if x > cols * gridSize then x:= attr.w - gridSize div 2;
  155.     if y > rows * gridSize then y:= attr.h - gridSize div 2;
  156.     pos.x := x;
  157.     pos.y := y;
  158.     clientToScreen(HWindow, pos);
  159.     setCursorPos(pos.x, pos.y);
  160. end;
  161.  
  162. ** Listing 5
  163. ** Responding to timer messages
  164.  
  165. { Create a display context for drawing and mutate the cells.
  166.   Use a white pen for the border, then set it back when done. }
  167. procedure TLifeWindow.wmTimer(var Msg: TMessage);
  168. begin
  169.   mutateDC:=getDC(HWindow);
  170.   selectObject(mutateDC, GetStockObject(White_Pen));
  171.   cells.mutate(mutateDC);
  172.   selectObject(mutateDC, GetStockObject(Black_Pen));
  173.   releaseDC(HWindow, mutateDC);
  174. end;
  175.